///////////////////////////////////////////////////////////////////////////////
//
//  Silverplot.js   			version 1.1
//
//  This file is provided to support hints, tips, hyperlinks and other actions
//  defined in SharpPlot and saved in XAML formatted output. It should be
//  referenced in your hosting webpage if you have used any of these features
//  in your charting.
// 
//  Copyright (c) 2007 Causeway Graphical Systems Ltd. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////

 var TIP_X_OFFSET = 0;   // Tip is spaced across from mouse
 var TIP_Y_OFFSET = 14;  // Tip is spaced below mouse
 var TIP_FONTSIZE = 13;  // Pixel size for the tip text
 var MAX_TIPWIDTH = 144; // Wrap tip text if wider than this
 var PENDING_TIP_INFO;   // Stone under which to hide tip information
 var TIP_TIMER = null;
 var TIP_TIMEOUT = 500;  // in ms (adjust to taste)
 
 function MD(s,e)  // Mouse-down may either jump or run an arbitrary handler
 {
   var tip,href,onclick; eval(s.Tag);
   if (href) window.location = href;
   else if (onclick) eval(onclick);  // Follow SVG naming here
 }

 function ME(s,e) // Mouse-enter does hints and tips and fakes the href hint
 {
   var hint,tip,href; eval(s.Tag);
   if (hint) status = hint; 
   if (href) { status = href; s.Cursor = "Hand"; }

   if (tip) // Set a timer running to show the tip
   {
     var xpos = e.getPosition(null).x;
     var ypos = e.getPosition(null).y; 
     PENDING_TIP_INFO={'sender':s, 'tip':tip, 'xpos':xpos,'ypos':ypos};
     TIP_TIMER = setTimeout('ShowTipDeferred()', TIP_TIMEOUT);
   }
 }
 
 function ShowTipDeferred(){
 	var ti = PENDING_TIP_INFO;
 	if(!ti) return;
 	PENDING_TIP_INFO = null;
 	ShowTip(ti['sender'], ti['tip'], ti['xpos'], ti['ypos']);
 }
 
 function ShowTip(s,tip,xpos,ypos)
 {
     var tipRect = s.findName('TipRect'); 
     var tipText = s.findName('TipField');
     
     var scalex = s.findName('STXY').ScaleX; // Will be 1.33 for pixels = points
     var scaley = s.findName('STXY').ScaleY;
     
     var fsize = TIP_FONTSIZE / scaley;      // Tip should not change size when we scale
     tipText.FontSize = fsize; 
     tipText["Canvas.Top"] = fsize * 0.33;   // Offsets are relative to fontsize
     tipText["Canvas.Left"] = fsize * 0.33;
     tipRect.StrokeThickness = 1 / scalex;   // Border-weight likewise
     
     tipText.Width = MAX_TIPWIDTH / scalex;
     tipText.Text = tip;
     var tipWidth  = tipText.ActualWidth  + fsize * 0.667; // Tip is at (3,3) in the canvas
     var tipHeight = tipText.ActualHeight + fsize * 0.6;   // A little less below looks better

     var tipper = s.findName('TipCanvas');   // Shift the whole caboodle now we know the details
     tipper["Canvas.Left"] = (xpos + TIP_X_OFFSET) / scalex; 
     tipper["Canvas.Top" ] = (ypos + TIP_Y_OFFSET) / scaley; 
     
     tipper.Width = tipWidth; 
     tipper.Height = tipHeight; 

     tipRect.Width = tipWidth; 
     tipRect.Height = tipHeight; 

     tipper.Visibility = "Visible";
 }

 function ML(s,e)  // Mouse-leave just clears up
 {
   var tip,href; eval(s.Tag);
   status = '';
   if (href) s.Cursor = "Default";
   if (tip) { 
   	s.findName('TipField').Text =''; s.findName('TipCanvas').Visibility = "Collapsed"; 
   	clearTimeout(TIP_TIMER);
   }
 }
 
 function MM(s,e) // Mouse-move adjusts the tip location
 {
   var tip,href; eval(s.Tag);
   if(tip && PENDING_TIP_INFO) { 
     PENDING_TIP_INFO['xpos'] = e.getPosition(null).x;
     PENDING_TIP_INFO['ypos'] = e.getPosition(null).y;  	
   }
 }